package cn.volunteer.points.system.controller;

import cn.volunteer.points.system.annotation.AdminLoginToken;
import cn.volunteer.points.system.annotation.LoginToken;
import cn.volunteer.points.system.annotation.PassToken;
import cn.volunteer.points.system.api.REST_URL;
import cn.volunteer.points.system.pojo.entity.Activity;
import cn.volunteer.points.system.pojo.entity.User;
import cn.volunteer.points.system.pojo.entity.UserActivity;
import cn.volunteer.points.system.pojo.qo.UserActivityQO;
import cn.volunteer.points.system.pojo.vo.ActivityVO;
import cn.volunteer.points.system.service.ActivityService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @description: 活动后端接口
 * @author: chenxiaofeng
 * @create: 创建时间：2021-04-24
 **/
@RestController
@Slf4j
public class ActivityController extends RestfulController{

    @Autowired
    private ActivityService activityService ;


    @ApiOperation("新增活动")
    @PostMapping(path = REST_URL.ACTIVITY_ADD)
    @AdminLoginToken
    public Response<?> addActivity(@RequestBody Activity activity){
        String add = activityService.add(activity);
        return success(activity ,add);
    }

    @ApiOperation("删除活动")
    @PostMapping(path = REST_URL.ACTIVITY_DEL)
    @AdminLoginToken
    public Response<?> delActivity(@RequestBody Activity activity){
        activityService.del(activity.getId());
        return success(activity.getId(),"活动删除成功！！！");
    }

    @ApiOperation("修改活动")
    @PostMapping(path = REST_URL.ACTIVITY_UPDATE)
    @AdminLoginToken
    public Response<?> updateActivity(@RequestBody Activity activity){
        String update = activityService.update(activity);
        return success(activity ,update);
    }

    @ApiOperation("活动详情")
    @PostMapping(path = REST_URL.ACTIVITY_INFO)
    @PassToken
    public Response<?> activityInfo(@RequestBody Activity activity){
        ActivityVO byId = activityService.findById(activity.getId());
        return success(byId);
    }

    @ApiOperation("活动列表")
    @PostMapping(path = REST_URL.ACTIVITY_LIST)
    @PassToken
    public Response<?> activityList(@RequestBody Activity activity){
        Integer status = activity.getStatus();
        List<ActivityVO> activities = activityService.activityList(status);
        return success(activities);
    }

    @ApiOperation("管理员活动列表")
    @PostMapping(path = REST_URL.ADMIN_ACTIVITY_LIST)
    @AdminLoginToken
    public Response<?> adminActivityList(@RequestBody Activity activity){
        Integer status = activity.getStatus();
        List<ActivityVO> activities = activityService.activityList(status);
        return success(activities);
    }


    @ApiOperation("用户参加、完成活动")
    @PostMapping(path = REST_URL.USER_ACTIVITY_FINISH)
    @LoginToken
    public Response<?> userActivity(@RequestBody UserActivity userActivity){
        String activity = activityService.userFinishActivity(userActivity);
        return success(userActivity,activity);
    }

    @ApiOperation("用户活动列表")
    @PostMapping(path = REST_URL.USER_ACTIVITY_INFO)
    @LoginToken
    public Response<?> userActivityList(@RequestBody User user){
        Integer userId = user.getId();
        List<Map> list = activityService.userActivityInfo(userId);
        return success(list);
    }

    @ApiOperation("用户取消已参加活动")
    @PostMapping(path = REST_URL.USER_CANCEL_ACTIVITY)
    @LoginToken
    public Response<?> userCancelActivtiy(@RequestBody UserActivityQO userActivityQO){
        String activtiy = activityService.userCancelActivtiy(userActivityQO);
        return success(userActivityQO,activtiy);
    }

    @ApiOperation("用户积分")
    @PostMapping(path = REST_URL.USER_INTEGRAL)
    @LoginToken
    public Response<?> userInitegral(@RequestBody Integer userId){
        Integer integral = activityService.getUserActivityIntegral(userId);
        return success(integral,"已获取用户积分") ;
    }


}
